SoSe2022
coord_trans() transformiert die Koordinaten des Plots entsprechend der angegebenen Transformation.scale_x_log10() und scale_y_log10() log-transformiert die X- und Y-Achse zur Basis 10.Hier ein Beispiel des möglichen Zusammenhangs zwischen dem Körpergewicht (in kg) und der Gehirnmasse (in g) von Säugetieren (Datensatz mammals aus dem Paket ‘MASS’):
MASS::mammals %>% ggplot(aes(x = body, y = brain)) + geom_point()
coord_trans()MASS::mammals %>% ggplot(aes(x = body, y = brain)) + geom_point() + coord_trans(x = "log10", y = "log10")
scale_x/y_log10()MASS::mammals %>% ggplot(aes(x = body, y = brain)) + geom_point() + scale_x_log10() + scale_y_log10()
→ Dies gilt allerdings nur bei linearen Zusammenhängen!
\[s^{2} = \frac{1}{n-1}\sum\limits_{i=1}^{n}(x_{i} - \bar{x})^2\]
\[s=\sqrt{s^{2}}\]
\[Cov(x,y)=\frac{1}{n-1}\sum\limits_{i=1}^{n}(x_i- \bar{x})(y_i- \bar{y})\]
\[Cov(X,Y)=\frac{1}{n-1}\sum\limits_{i=1}^{n}(x_i- \bar{x})(y_i- \bar{y})\]
\[ Cov(x,y)_{max} = s_x*s_y\]
\[r_{x,y} = \frac{Cov(x,y)}{Cov(x,y)_{max}} = \frac{\frac{1}{n-1}\sum\limits_{i=1}^{n}(x_i- \bar{x})(y_i- \bar{y})}{s_x*s_y}\]
\[r_s = 1-\frac{6\sum D_i^2}{n(n^2-1)}~~~\text{mit n = Anzahl der Rangpaare}\]
cor(x, y, method = "pearson") # oder alternativ (weil 'pearson' Standard ist): cor(x, y)
x und y sind in diesem Falle einzelne Vektoren.cor(x = iris$Sepal.Length, y = iris$Sepal.Width)
[1] -0.1175698
x bzw. x und y übergeben werden (beide müssen numerisch sein!).
cor(x = iris[ ,1:4]) # wichtig: ohne 'species'
Sepal.Length Sepal.Width Petal.Length Petal.Width
Sepal.Length 1.0000000 -0.1175698 0.8717538 0.8179411
Sepal.Width -0.1175698 1.0000000 -0.4284401 -0.3661259
[ erreichte getOption("max.print") -- 2 Zeilen ausgelassen ]Um zu testen, ob die Beziehung signifikant ist, kann die Funktion cor() erweitert werden zu cor.test():
cor.test(x, y, method = "pearson") # oder einfach nur cor.test(x, y)
cor.test(x = iris$Sepal.Length, y = iris$Sepal.Width)
Pearson's product-moment correlation
data: iris$Sepal.Length and iris$Sepal.Width
t = -1.4403, df = 148, p-value = 0.1519
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
-0.27269325 0.04351158
sample estimates:
cor
-0.1175698 sl <- iris$Sepal.Length sw <- iris$Sepal.Width cor(x = sl, y = sw, method = "pearson")
[1] -0.1175698
cor(x = sl, y = sw, method = "spearman")
[1] -0.1667777
cor(x = sl, y = sw, method = "kendall")
[1] -0.07699679
cor.test(x = sl, y = sw, method = "spearman")
Spearman's rank correlation rho
data: sl and sw
S = 656283, p-value = 0.04137
alternative hypothesis: true rho is not equal to 0
sample estimates:
rho
-0.1667777
cor.test(x = sl, y = sw, method = "kendall")
Kendall's rank correlation tau
data: sl and sw
z = -1.3318, p-value = 0.1829
alternative hypothesis: true tau is not equal to 0
sample estimates:
tau
-0.07699679 use → wähle hier ‘pairwise.complete.obs’ um die paarweisen Werte zu entfernen wenn ein NA vorkommt.sl[1] <- NA cor(x = sl, y = sw, method = "pearson")
[1] NA
cor(x = sl, y = sw, method = "pearson", use = "pairwise.complete.obs")
[1] -0.1121059
Skalenniveaus lassen sich umwandeln, allerdings kann dies immer mit einem Informationsverlust einhergehen.
x <- c(0.5, 0.2, 0.3) x_arc <- asin(sqrt(x))
x <- c(3,1,1,5,7,42) x_2 <- sqrt(x) # oder: x^0.5 x_4 <- sqrt(sqrt(x)) # oder: x^0.25
x <- c(0, 0.5, 7.8, 9.4) # natürlicher Log: x_ln <- log(x + 0.1) # Log zur Basis 10: x_log10 <- log10(x + 0.1)
Bei weiteren Fragen: saskia.otto(at)uni-hamburg.de

Diese Arbeit is lizenziert unter einer Creative Commons Attribution-ShareAlike 4.0 International License mit Ausnahme der entliehenen und mit Quellenangabe versehenen Angaben.